package org.gdstash.ui.character;

import org.gdstash.character.GDChar;
import org.gdstash.character.GDCharShrineList;
import org.gdstash.character.GDCharUID;
import org.gdstash.db.DBShrine;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.util.GDAbstractShrinePane;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.List;

public class GDCharShrineAct3Pane extends GDAbstractShrinePane {
    private JCheckBox cbMountainDeeps;
    private JCheckBox cbForgottenDepths;
    private JCheckBox cbTyrantsHold;
    private JCheckBox cbInfestedFarms;
    private JCheckBox cbDenOfTheLost;
    private JCheckBox cbConflagration;
    private JCheckBox cbPortValbury;
    private JCheckBox cbBloodGrove;
    private JCheckBox cbStonerendQuarry;
    private JCheckBox cbVillageOfDarkvale;
    private int difficulty;
    private boolean changed;

    public GDCharShrineAct3Pane(final int difficulty, final int direction) {
        this.difficulty = difficulty;
        this.changed = false;
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.cbMountainDeeps).addComponent(this.cbForgottenDepths).addComponent(this.cbTyrantsHold).addComponent(this.cbInfestedFarms).addComponent(this.cbDenOfTheLost).addComponent(this.cbConflagration).addComponent(this.cbPortValbury).addComponent(this.cbBloodGrove).addComponent(this.cbStonerendQuarry).addComponent(this.cbVillageOfDarkvale));
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.cbMountainDeeps)).addGroup(layout.createParallelGroup().addComponent(this.cbForgottenDepths)).addGroup(layout.createParallelGroup().addComponent(this.cbTyrantsHold)).addGroup(layout.createParallelGroup().addComponent(this.cbInfestedFarms)).addGroup(layout.createParallelGroup().addComponent(this.cbDenOfTheLost)).addGroup(layout.createParallelGroup().addComponent(this.cbConflagration)).addGroup(layout.createParallelGroup().addComponent(this.cbPortValbury)).addGroup(layout.createParallelGroup().addComponent(this.cbBloodGrove)).addGroup(layout.createParallelGroup().addComponent(this.cbStonerendQuarry)).addGroup(layout.createParallelGroup().addComponent(this.cbVillageOfDarkvale));
        if (direction == 0) {
            layout.setHorizontalGroup(vGroup);
            layout.setVerticalGroup(hGroup);
        } else {
            layout.setHorizontalGroup(hGroup);
            layout.setVerticalGroup(vGroup);
        }
        layout.linkSize(0, this.cbMountainDeeps, this.cbForgottenDepths);
        layout.linkSize(0, this.cbMountainDeeps, this.cbTyrantsHold);
        layout.linkSize(0, this.cbMountainDeeps, this.cbInfestedFarms);
        layout.linkSize(0, this.cbMountainDeeps, this.cbDenOfTheLost);
        layout.linkSize(0, this.cbMountainDeeps, this.cbConflagration);
        layout.linkSize(0, this.cbMountainDeeps, this.cbPortValbury);
        layout.linkSize(0, this.cbMountainDeeps, this.cbBloodGrove);
        layout.linkSize(0, this.cbMountainDeeps, this.cbStonerendQuarry);
        layout.linkSize(0, this.cbMountainDeeps, this.cbVillageOfDarkvale);
        layout.linkSize(1, this.cbMountainDeeps, this.cbForgottenDepths);
        layout.linkSize(1, this.cbMountainDeeps, this.cbTyrantsHold);
        layout.linkSize(1, this.cbMountainDeeps, this.cbInfestedFarms);
        layout.linkSize(1, this.cbMountainDeeps, this.cbDenOfTheLost);
        layout.linkSize(1, this.cbMountainDeeps, this.cbConflagration);
        layout.linkSize(1, this.cbMountainDeeps, this.cbPortValbury);
        layout.linkSize(1, this.cbMountainDeeps, this.cbBloodGrove);
        layout.linkSize(1, this.cbMountainDeeps, this.cbStonerendQuarry);
        layout.linkSize(1, this.cbMountainDeeps, this.cbVillageOfDarkvale);
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntCheck = UIManager.getDefaults().getFont("CheckBox.font");
        if (fntCheck == null) {
            fntCheck = fntLabel;
        }
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntCheck = fntCheck.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final TitledBorder text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_ACT3"));
        text.setTitleFont(fntBorder);
        this.setBorder(text);
        if (this.cbMountainDeeps == null) {
            (this.cbMountainDeeps = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbMountainDeeps.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_03_MOUNTAIN_DEEPS"));
        this.cbMountainDeeps.setFont(fntCheck);
        if (this.cbForgottenDepths == null) {
            (this.cbForgottenDepths = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbForgottenDepths.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_03_FORGOTTEN_DEPTHS"));
        this.cbForgottenDepths.setFont(fntCheck);
        if (this.cbTyrantsHold == null) {
            (this.cbTyrantsHold = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbTyrantsHold.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_03_TYRANTS_HOLD"));
        this.cbTyrantsHold.setFont(fntCheck);
        if (this.cbInfestedFarms == null) {
            (this.cbInfestedFarms = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbInfestedFarms.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_03_INFESTED_FARMS"));
        this.cbInfestedFarms.setFont(fntCheck);
        if (this.cbDenOfTheLost == null) {
            (this.cbDenOfTheLost = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbDenOfTheLost.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_03_DEN_OF_THE_LOST"));
        this.cbDenOfTheLost.setFont(fntCheck);
        if (this.cbConflagration == null) {
            (this.cbConflagration = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbConflagration.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_03_CONFLAGRATION"));
        this.cbConflagration.setFont(fntCheck);
        if (this.cbPortValbury == null) {
            (this.cbPortValbury = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbPortValbury.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_03_PORT_VALBURY"));
        this.cbPortValbury.setFont(fntCheck);
        if (this.cbBloodGrove == null) {
            (this.cbBloodGrove = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbBloodGrove.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_03_BLOOD_GROVE"));
        this.cbBloodGrove.setFont(fntCheck);
        if (this.cbStonerendQuarry == null) {
            (this.cbStonerendQuarry = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbStonerendQuarry.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_03_STONEREND_QUARRY"));
        this.cbStonerendQuarry.setFont(fntCheck);
        if (this.cbVillageOfDarkvale == null) {
            (this.cbVillageOfDarkvale = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbVillageOfDarkvale.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_03_VILLAGE_OF_DARKVALE"));
        this.cbVillageOfDarkvale.setFont(fntCheck);
    }

    @Override
    public boolean hasChanged() {
        return this.changed;
    }

    @Override
    public void setChanged(final boolean changed) {
        this.changed = changed;
    }

    public void setSelected(final boolean selected) {
        this.cbMountainDeeps.setSelected(selected);
        this.cbForgottenDepths.setSelected(selected);
        this.cbTyrantsHold.setSelected(selected);
        this.cbInfestedFarms.setSelected(selected);
        this.cbDenOfTheLost.setSelected(selected);
        this.cbConflagration.setSelected(selected);
        this.cbPortValbury.setSelected(selected);
        this.cbBloodGrove.setSelected(selected);
        this.cbStonerendQuarry.setSelected(selected);
        this.cbVillageOfDarkvale.setSelected(selected);
    }

    @Override
    public List<GDCharUID> getShrineList(final boolean found) {
        final List<GDCharUID> list = new LinkedList<GDCharUID>();
        if (this.cbMountainDeeps.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_03_MOUNTAIN_DEEPS);
        }
        if (this.cbForgottenDepths.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_03_FORGOTTEN_DEPTHS);
        }
        if (this.cbTyrantsHold.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_03_TYRANTS_HOLD);
        }
        if (this.cbInfestedFarms.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_03_INFESTED_FARMS);
        }
        if (this.cbDenOfTheLost.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_03_DEN_OF_THE_LOST);
        }
        if (this.cbConflagration.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_03_CONFLAGRATION);
        }
        if (this.cbPortValbury.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_03_PORT_VALBURY);
        }
        if (this.cbBloodGrove.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_03_BLOOD_GROVE);
        }
        if (this.cbStonerendQuarry.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_03_STONEREND_QUARRY);
        }
        if (this.cbVillageOfDarkvale.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_03_VILLAGE_OF_DARKVALE);
        }
        return list;
    }

    @Override
    public void setChar(final GDChar gdc) {
        this.setSelected(false);
        this.changed = false;
        boolean expFG = GDStashFrame.expansionForgottenGods;
        if (gdc != null) {
            expFG = (expFG || gdc.isForgottenGodsChar());
        }
        DBShrine.Info info = null;
        info = DBShrine.getShrineInfo("devotionshrinec01.dbr", this.difficulty);
        this.cbMountainDeeps.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrinec02.dbr", this.difficulty);
        this.cbForgottenDepths.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrinec03.dbr", this.difficulty);
        this.cbTyrantsHold.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrinec05.dbr", this.difficulty);
        this.cbInfestedFarms.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrinec04.dbr", this.difficulty);
        this.cbDenOfTheLost.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrinen12.dbr", this.difficulty);
        this.cbConflagration.setEnabled(info.active);
        this.cbConflagration.setVisible(info.exists || expFG);
        info = DBShrine.getShrineInfo("devotionshrinen01.dbr", this.difficulty);
        this.cbPortValbury.setEnabled(info.active);
        this.cbPortValbury.setVisible(info.exists || expFG);
        info = DBShrine.getShrineInfo("devotionshrinec08.dbr", this.difficulty);
        this.cbBloodGrove.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrinen05.dbr", this.difficulty);
        this.cbStonerendQuarry.setEnabled(info.active);
        this.cbStonerendQuarry.setVisible(info.exists || expFG);
        info = DBShrine.getShrineInfo("devotionshrinec06.dbr", this.difficulty);
        this.cbVillageOfDarkvale.setEnabled(info.active);
        if (gdc == null) {
            return;
        }
        final List<GDCharUID> list = gdc.getRestoredShrinesList(this.difficulty);
        for (final GDCharUID uid : list) {
            if (uid.equals(GDCharShrineList.UID_SHRINE_03_MOUNTAIN_DEEPS)) {
                this.cbMountainDeeps.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_03_FORGOTTEN_DEPTHS)) {
                this.cbForgottenDepths.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_03_TYRANTS_HOLD)) {
                this.cbTyrantsHold.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_03_INFESTED_FARMS)) {
                this.cbInfestedFarms.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_03_DEN_OF_THE_LOST)) {
                this.cbDenOfTheLost.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_03_CONFLAGRATION)) {
                this.cbConflagration.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_03_PORT_VALBURY)) {
                this.cbPortValbury.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_03_BLOOD_GROVE)) {
                this.cbBloodGrove.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_03_STONEREND_QUARRY)) {
                this.cbStonerendQuarry.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_03_VILLAGE_OF_DARKVALE)) {
                this.cbVillageOfDarkvale.setSelected(true);
            }
        }
    }

    private class ChangeActionListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent e) {
            GDCharShrineAct3Pane.this.changed = true;
        }
    }
}
